﻿// 项目：	Guan.CMS.CommandProcessor - Guan.CMS
// 文件名：	DefaultCommandBus.cs
// 
// 修改时间：2013-03-06 19:26
// 修改人：	Cooclsee

using System.Collections.Generic;
using System.Web.Mvc;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Core.Common;

namespace Guan.CMS.CommandProcessor.Dispatcher
{
    public class DefaultCommandBus : ICommandBus
    {
        public ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand
        {
            var handler = DependencyResolver.Current.GetService<ICommandHandler<TCommand>>();
            if (handler == null)
            {
                throw new CommandHandlerNotFoundException(typeof (TCommand));
            }
            return handler.Execute(command);
        }

        public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand
        {
            var handler = DependencyResolver.Current.GetService<IValidationHandler<TCommand>>();
            if (handler == null)
            {
                throw new ValidationHandlerNotFoundException(typeof (TCommand));
            }
            return handler.Validate(command);
        }
    }
}